//
//  StatusOriginalView.swift
//  Weibo
//
//  Created by 孙成宇 on 15/11/30.
//  Copyright © 2015年 mac. All rights reserved.
//

/*

1.准备顶部视图
2.懒加载控件
3.设置界面
4.添加控件和自动布局
5.设置顶部视图数据



*/

import UIKit

class StatusOriginalView: UIView {


    private var bottomconstraint: Constraint?
    //MARK : 1.准备顶部视图
 
    
    //MARK: 5.设置顶部视图数据
    var status: Status? {
        didSet {
            headImage.sd_setImageWithURL(status?.user?.headImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
          
            nameLabel.text = status?.user?.name
          
            mbrankImage.image = status?.user?.mbrankImage
            
            
            verified_type_image.image = status?.user?.verified_type_image
            
            contentLabel.text = status?.text
            
            pictureView.imageURLs = status?.imageURLs
            
            
            bottomconstraint?.uninstall()
            
            
            if let urls = status?.imageURLs where urls.count != 0 {
            
            pictureView.imageURLs = urls

                self.snp_updateConstraints(closure: { (make) -> Void in
                 self.bottomconstraint =  make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                    
                })
            
            pictureView.hidden = false
            
            }else{
            
            self.snp_updateConstraints(closure: { (make) -> Void in
               self.bottomconstraint =  make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
            })
            
            pictureView.hidden = true
            }
            
        
        
        }
    

    }
    
    
    
    
    

 //MARK: 3.设置界面
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK:4.添加控件和自动布局
    private func setupUI() {
      sepVIew.backgroundColor = UIColor.lightGrayColor()
       addSubview(sepVIew)
      
        addSubview(headImage)
    
        addSubview(contentLabel)
        
        addSubview(nameLabel)
        
        addSubview(mbrankImage)
        
        addSubview(verified_type_image)
        
        addSubview(sourceLabel)
        
        addSubview(timeLabel)
        
        addSubview(pictureView)
        

        
        //设置布局
      sepVIew.snp_makeConstraints { (make) -> Void in
      make.top.left.right.equalTo(self)

      make.height.equalTo(StatusCellMargin)

        }



        headImage.snp_makeConstraints { (make) -> Void in
            make.top.left.equalTo(sepVIew.snp_bottom).offset(StatusCellMargin)
            
           
            make.width.equalTo(50)
           
            make.height.equalTo(50)
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(headImage.snp_right).offset(StatusCellMargin)
            make.top.equalTo(headImage.snp_top)
            
        }
        
        mbrankImage.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
            make.top.equalTo(nameLabel.snp_top)
        }
        
        verified_type_image.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImage.snp_right)
            make.centerY.equalTo(headImage.snp_bottom)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(headImage.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(headImage.snp_bottom)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(timeLabel.snp_bottom)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.top.equalTo(headImage.snp_bottom).offset(StatusCellMargin)
        }
        
        
        
        //MARK :  配图视图
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            
            make.left.equalTo(contentLabel.snp_left)
            
           
//            make.height.equalTo(100)
//            make.width.equalTo(100)
        
        }
        
        
        self.snp_makeConstraints { (make) -> Void in
          self.bottomconstraint =  make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    }
     //MARK: 2.懒加载控件
    
    
    private lazy var headImage: UIImageView = UIImageView()
    
    private lazy var nameLabel: UILabel = UILabel(title: "我就是哥", color: UIColor.darkGrayColor(), fontSize: 14)
    
    private lazy var verified_type_image: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    private lazy var mbrankImage: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    private lazy var timeLabel: UILabel = UILabel(title: "12:12", color: UIColor.orangeColor(), fontSize: 10)
    
    private lazy var sourceLabel: UILabel = UILabel(title: "隔壁", color: UIColor.darkGrayColor(), fontSize: 10)
    private lazy var contentLabel: UILabel = UILabel(title: "我就是我", color: UIColor.darkGrayColor(), fontSize: 14 , margin: StatusCellMargin)
    
    
    
    //配图视图
    private lazy var pictureView : StatusPictureView = StatusPictureView()


    private lazy var sepVIew: UIView = UIView()

}
